package org.gtesoreria

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

class ReportService {

    static transactional = true
	static final WritableFont FONT_ARIAL_12 = new WritableFont(WritableFont.ARIAL, 12)
	static final WritableFont FONT_ARIAL_16 = new WritableFont(WritableFont.ARIAL, 16)
	static final WritableFont FONT_TAHOMA_20 = new WritableFont(WritableFont.TAHOMA, 20)
	static final WritableCellFormat FORMAT_ARIAL_12 = new WritableCellFormat(FONT_ARIAL_12)
	static final WritableCellFormat FORMAT_ARIAL_20 = new WritableCellFormat(FONT_TAHOMA_20)
	
	public File createReport(List lista, List labels, String titulo) {
			WorkbookSettings workbookSettings = new WorkbookSettings()
			workbookSettings.locale = Locale.default
	
			def file = File.createTempFile('myExcelDocument', '.xls')
			//file.deleteOnExit()
	
			WritableWorkbook workbook = Workbook.createWorkbook(file, workbookSettings)
			
			WritableSheet sheet = workbook.createSheet('MySheet', 0)
			def col = 0;
			sheet.addCell(new Label(0, 0, titulo, FORMAT_ARIAL_20))
			for (String label : labels) {
				sheet.addCell(new Label(col++, 1, label, FORMAT_ARIAL_12))
			}
			def row= 2;
			for(def objectRow:lista){
				col = 0;
				def props = objectRow.printableProperties()
				sheet.addCell(new Label(col, row, (row-1).toString() , FORMAT_ARIAL_12))
				for(String prop:props)
					sheet.addCell(new Label(++col, row, prop, FORMAT_ARIAL_12))
				row++
			}
			workbook.write()
			workbook.close()
			return file
		}
	
	public File createReportDescuentos(def dataMap, List labels, String titulo) {
		WorkbookSettings workbookSettings = new WorkbookSettings()
		workbookSettings.locale = Locale.default

		def file = File.createTempFile('myExcelDocument', '.xls')
		//file.deleteOnExit()

		WritableWorkbook workbook = Workbook.createWorkbook(file, workbookSettings)
		
		WritableSheet sheet = workbook.createSheet('MySheet', 0)
		def col = 0;
		
		sheet.addCell(new Label(0, 0, titulo, FORMAT_ARIAL_20))
		for(String keyData in dataMap.keySet()){
			if(dataMap[keyData])
				sheet = printServicioDescuento(sheet, dataMap[keyData], keyData)
		}
		workbook.write()
		workbook.close()
		return file
	}
	
	public WritableSheet printServicioDescuento(WritableSheet sheet, def lista, String dataLabel){
		def col = 0;
		def row = sheet.getRows();
		def labels=["Legajo","Apellido y Nombre",dataLabel,"Cant. de Cuotas","Importe Cuota","Codigo de Desc."]
		for (String label : labels) {
			sheet.addCell(new Label(col++, row + 1, label, FORMAT_ARIAL_12))
		}
		row = row + 2
		for(def objectRow:lista){
			col = 0;
			def props = printableProperties(objectRow)
			
			for(def prop:props){
				if(prop instanceof String)
					sheet.addCell(new Label(col++, row, prop, FORMAT_ARIAL_12))
				else {
					sheet.addCell(new Number(col++, row, prop, FORMAT_ARIAL_12))
				}
			}
			row++
		}
		return sheet
	}
	
	public printableProperties(def object){
		DecimalFormat df = new DecimalFormat("##.00");
		if(object instanceof Adelanto){
			Adelanto adelanto = (Adelanto) object
			return [adelanto.afiliado.legajo, adelanto.afiliado.apellidoNombre,"","1",adelanto.montoTotal,Adelanto.CODIGO]
		} else if(object instanceof Consumo){
			Consumo consumo = (Consumo) object
			return [consumo.afiliado.legajo, consumo.afiliado.apellidoNombre,"","1",consumo.monto,consumo.proveedor.rubro.codigo]
		} else if(object instanceof Cuota){
			Cuota cuota = (Cuota) object
			return [cuota.credito.afiliado.legajo, cuota.credito.afiliado.apellidoNombre,"","1",cuota.monto,Prestamo.CODIGO]
		}
	}
	
	public File createReportAdelantos(def data, List labels, String titulo){
		WorkbookSettings workbookSettings = new WorkbookSettings()
		workbookSettings.locale = Locale.default

		def file = File.createTempFile('myExcelDocument', '.xls')
		//file.deleteOnExit()

		WritableWorkbook workbook = Workbook.createWorkbook(file, workbookSettings)
		
		WritableSheet sheet = workbook.createSheet('MySheet', 0)
		def col = 0;
		sheet.addCell(new Label(0, 0, titulo, FORMAT_ARIAL_20))
		for (String label : labels) {
			sheet.addCell(new Label(col++, 1, label, FORMAT_ARIAL_12))
		}
		def row= 2;
		SimpleDateFormat formatoDeFecha = new SimpleDateFormat("MM/yyyy");
		for(Adelanto objectRow:data){
			col = 0;
			def props = [objectRow.afiliado.legajo, objectRow.afiliado.apellidoNombre, objectRow.getMontoTotal().toString(), formatoDeFecha.format(objectRow.fechaPedido), objectRow.entrego.apellidoNombre]
			sheet.addCell(new Label(col, row, (row-1).toString() , FORMAT_ARIAL_12))
			for(String prop:props)
				sheet.addCell(new Label(++col, row, prop, FORMAT_ARIAL_12))
			row++
		}
		workbook.write()
		workbook.close()
		return file
	}
}
